﻿using HandyControl.Controls;
using Microsoft.Win32;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Input;

namespace BaseUi.ViewModel
{
    public class SetViewModel : BindableBase
    {
        private List<int> SaveLoginCountData_ = new List<int> { 3, 5, 7, 10 };
        //记忆最大数量下拉框
        public List<int> SaveLoginCountData { get => SaveLoginCountData_; set { SetProperty(ref SaveLoginCountData_, value); } }

        private Dictionary<int, string> LogTimeData_ = new Dictionary<int, string>()
        {
            { 30,"一月"},{ 91,"三月"},{ 182,"半年"},{ 365,"一年"},{ 1095,"三年"},{ -1,"永久"},
        };
        //日志缓存时间下拉框
        public Dictionary<int, string> LogTimeData { get => LogTimeData_; set { SetProperty(ref LogTimeData_, value); } }


        #region 页面输入信息

        private bool Powerboot_;
        public bool Powerboot { get => Powerboot_; set { SetProperty(ref Powerboot_, value); } }

        private bool StartupFull_;
        public bool StartupFull { get => StartupFull_; set { SetProperty(ref StartupFull_, value); } }

        private bool IsSaveLogin_;
        public bool IsSaveLogin { get => IsSaveLogin_; set { SetProperty(ref IsSaveLogin_, value); } }

        private int SaveLoginCount_;
        public int SaveLoginCount { get => SaveLoginCount_; set { SetProperty(ref SaveLoginCount_, value); } }

        private int SaveLogDay_;
        public int SaveLogDay { get => SaveLogDay_; set { SetProperty(ref SaveLogDay_, value); } }
        #endregion

        /// <summary>
        /// 刷新界面
        /// </summary>
        public void Update()
        {
            try
            {
                LocalFile.UpdateConfig();
                Powerboot = LocalFile.Config.Sys.Powerboot;
                StartupFull = LocalFile.Config.Sys.StartupFull;
                IsSaveLogin = LocalFile.Config.Sys.IsSaveLogin;
                SaveLoginCount = LocalFile.Config.Sys.SaveLoginCount;
                SaveLogDay = LocalFile.Config.Sys.SaveLogDay;
            }
            catch (Exception ex)
            {
                Growl.Info($"获取配置失败。{ex.Message}");
            }
        }

        public ICommand SaveCommand { get => new DelegateCommand(Save); }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            try
            {
                LocalFile.Config.Sys.Powerboot = Powerboot;
                LocalFile.Config.Sys.StartupFull = StartupFull;
                LocalFile.Config.Sys.IsSaveLogin = IsSaveLogin;
                LocalFile.Config.Sys.SaveLoginCount = SaveLoginCount;
                LocalFile.Config.Sys.SaveLogDay = SaveLogDay;
                LocalFile.SaveConfig();
                Growl.Success($"保存成功。");

                RunPowerboot(Powerboot);
            }
            catch (Exception ex)
            {
                LocalFile.UpdateConfig();
                Growl.Error($"保存失败。{ex.Message}");
            }
        }

        private void RunPowerboot(bool isPowerboot)
        {
            try
            {
                if (isPowerboot)
                {
                    RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (rgkRun == null)
                        rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

                    rgkRun.SetValue(Path.GetFileNameWithoutExtension(LocalFile.AppName), "\"" + Path.Combine(LocalFile.AppDir, LocalFile.AppName) + "\"");
                }
                else
                {
                    RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (rgkRun == null)
                        rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

                    rgkRun.DeleteValue(Path.GetFileNameWithoutExtension(LocalFile.AppName), false);
                }
            }
            catch (Exception ex)
            {
                Logs.Write(ex);
            }
        }

    }
}
